package com.zm.common.utils.collection;

import com.zm.common.utils.object.ObjectUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class MapUtils extends org.apache.commons.collections.MapUtils {

//        <dependency>
//            <groupId>commons-collections</groupId>
//            <artifactId>commons-collections</artifactId>
//            <version>3.2.2</version>
//        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.4</version>
        </dependency>

    public static Map<Object, Object> objToMap(Object obj) {
        return new org.apache.commons.beanutils.BeanMap(obj);
    }

    /**
     * @param obj
     * @param isFilter 是否过滤 value 为空的 key
     * @return
     * @author 刘洋
     * @createDate 2017年2月10日
     */
    public static Map<String, Object> objToMap(Object obj, boolean isFilter) {
        if (null == obj) {
            return null;
        }
        Map<Object, Object> map = new org.apache.commons.beanutils.BeanMap(obj);
        Map<String, Object> result = new HashMap<>();
        for (Entry<Object, Object> entry : map.entrySet()) {
            Object value = entry.getValue();
            String key = entry.getKey().toString();
            if (isFilter) {
                if (ObjectUtils.isNotNull(value)) {
                    result.put(key, value);
                }
            } else {
                result.put(key, value);
            }
        }
        map.clear();
        return result;
    }

}
